package com.dimeng;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.dimeng.service.DownloadService;
import com.dimeng.utils.Constant;

/**
 * @author write by ccn<br>
 *         <b>helloandroid100@gmail.com</b>
 * @version 0.1;2012-7-27 下午2:50:54<br>
 *          descrition:
 */
public class NotificationActivity extends SuperActivity implements
		OnClickListener {
	private static final long serialVersionUID = 9016322517761458441L;

	private Button startServiceButton;
	private Button stopServiceButton;
	private Button bindServiceButton;
	private Button unbindServiceButton;

	private DownloadService mMyService;

	// 这里需要用到ServiceConnection在Context.bindService和context.unBindService()里用到
	private ServiceConnection mServiceConnection = new ServiceConnection() {
		// 当我bindService时
		public void onServiceConnected(ComponentName name, IBinder service) {
			mMyService = ((DownloadService.LocalBinder) service).getService();
		}

		public void onServiceDisconnected(ComponentName name) {
			if(null != mMyService)
				mMyService.stopSelf();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notification_test);
		startServiceButton = (Button) findViewById(R.id.startServiceId);
		stopServiceButton = (Button) findViewById(R.id.stopServiceId);
		bindServiceButton = (Button) findViewById(R.id.bindServiceId);
		unbindServiceButton = (Button) findViewById(R.id.unbindServiceId);

		startServiceButton.setOnClickListener(this);
		stopServiceButton.setOnClickListener(this);
		bindServiceButton.setOnClickListener(this);
		unbindServiceButton.setOnClickListener(this);
	}

	public void onClick(View v) {
		Intent intent = new Intent(NotificationActivity.this,
				DownloadService.class);
		if (v == startServiceButton) {
			String url = "http://img1.taojindi.com/mob/apk/taojindi1.0.5.apk";
	        intent.putExtra("url", url);
	        String saveFileNamePath = Constant.LOACL_APK_SAVE_PATH+url.substring(url.lastIndexOf("/")+1);
	        System.out.println(saveFileNamePath);
	        intent.putExtra("saveFileNamePath", saveFileNamePath);
			NotificationActivity.this.startService(intent);
		} else if (v == stopServiceButton) {
			NotificationActivity.this.stopService(intent);
		} else if (v == bindServiceButton) {
			NotificationActivity.this.bindService(intent, mServiceConnection, 1);
		} else if (v == unbindServiceButton) {
			NotificationActivity.this.unbindService(mServiceConnection);
		}
	}
}
